package com.isoftstone.industry.aaa.core.dao.assist;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.isoftstone.industry.persistance.bean.TicketInfo;
import com.isoftstone.modules.core.persistence.IGenericDao;

public interface TicketInfoDao  extends IGenericDao<TicketInfo, String>{
	@Query("select x from TicketInfo x where x.appInstance.id=?1 and x.chargeMode=?2 and x.charged=?3 order by x.createdTime asc")
	List<TicketInfo> findByAppInstAndChargedAndChargeMode(String appInstId, String chargeMode, Boolean charged);
	
	@Modifying
	@Query("update TicketInfo x set x.charged=:charged, x.chargedTime=:chargedTime where x.id=:id")
	void changeChargedProp(@Param("id") String id, @Param("charged") Boolean charged, @Param("chargedTime") Timestamp chargedTime);

	@Query("select x from TicketInfo x where x.appInstance.id=?1 and x.chargeMode=?2 and x.charged=?3 and (x.createdTime >=?4 and x.createdTime <=?5) order by x.createdTime asc")
	List<TicketInfo> findByAppInstAndChargedAndChargeModeAndCreatedTimeBetween(String appInstId, String chargeMode, Boolean charged, Timestamp start, Timestamp end);
	
	@Modifying
	@Query("update TicketInfo x set x.charged=:charged, x.chargedTime=:chargedTime, x.chargeOrder=:chargeOrder where x.id=:id")
	void changeChargedAndChargeOrderProp(@Param("id") String id, @Param("charged") Boolean charged, @Param("chargeOrder")long chargeOrder, @Param("chargedTime") Timestamp chargedTime);

	@Query("select count(*) from TicketInfo x where x.charged=:charged and x.appInstance.id=:appInstId and x.chargeMode=:chargeMode")
	long countByCharged(@Param("appInstId")String appInstId, @Param("chargeMode")String chargeMode, @Param("charged")Boolean charged);
	
	@Query("select count(*) from TicketInfo x where x.charged=:charged and x.appInstance.id=:appInstId and x.chargeMode=:chargeMode and (x.createdTime>=:start and x.createdTime<=:end)")
	long countByChargedAndCreatedTimeBetween(@Param("appInstId")String appInstId, @Param("chargeMode")String chargeMode, @Param("charged")Boolean charged, @Param("start")Timestamp start, @Param("end")Timestamp end);	
	
	// 2015-04 产品包 start
	@Query("select x from TicketInfo x where x.appInstance.id in (?1) and x.chargeMode=?2 and x.charged=?3 order by x.createdTime asc")
	List<TicketInfo> findByAppInstsAndChargedAndChargeMode(List<String> appInstIds, String chargeMode, Boolean charged);
	
	@Query("select x from TicketInfo x where x.appInstance.id in (?1) and x.chargeMode=?2 and x.charged=?3 and (x.createdTime >=?4 and x.createdTime <=?5) order by x.createdTime asc")
	List<TicketInfo> findByAppInstsAndChargedAndChargeModeAndCreatedTimeBetween(List<String> appInstIds, String chargeMode, Boolean charged, Timestamp start, Timestamp end);
	
	@Query("select count(*) from TicketInfo x where x.charged=:charged and x.appInstance.id in (:appInstIds) and x.chargeMode=:chargeMode")
	long countByAppInstsAndCharged(@Param("appInstIds")List<String> appInstIds, @Param("chargeMode")String chargeMode, @Param("charged")Boolean charged);
	
	@Query("select count(*) from TicketInfo x where x.charged=:charged and x.appInstance.id in (:appInstIds) and x.chargeMode=:chargeMode and (x.createdTime>=:start and x.createdTime<=:end)")
	long countByAppInstsAndChargedAndCreatedTimeBetween(@Param("appInstIds")List<String> appInstIds, @Param("chargeMode")String chargeMode, @Param("charged")Boolean charged, @Param("start")Timestamp start, @Param("end")Timestamp end);	
	// 2015-04 产品包 end
}
